package com.qu.waibao.publisher;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseActivity;
import com.qu.waibao.base.BaseRecyclerAdapter;
import com.qu.waibao.bean.InitService;
import com.qu.waibao.interf.XnetCallBack;
import com.qu.waibao.network.ActionUtil;
import com.qu.waibao.network.NetManager;
import com.qu.waibao.utils.GsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class InitServiceActivity extends BaseActivity implements View.OnClickListener,
        BaseRecyclerAdapter.OnItemClickListener{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_publish)
    ImageView iv_publish;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private InitAdapter adapter;
    @Override
    protected int getContentView() {
        return R.layout.activity_init_service;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        tv_title.setText("服务类型");
        iv_publish.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new InitAdapter(this, BaseRecyclerAdapter.NEITHER);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        initPage();
    }
    @OnClick({R.id.ll_back})
    @Override
    public void onClick(View view) {
       switch (view.getId()){
           case R.id.ll_back:
               finish();
               break;
       }
    }

    public void initPage(){
        Map<String,String> map = new HashMap<>();
        map.put("action", ActionUtil.servicecategory_init_get);
        NetManager.getInstance().request(this, map, true, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {

                List<InitService> list = GsonUtil.createGson().fromJson(result,
                        new TypeToken<List<InitService>>(){}.getType());
               if(list != null){
                   adapter.clear();
                   adapter.addAll(list);
               }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onItemClick(int position, long itemId) {
        InitService initService = adapter.getItems().get(position);
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("InitService",initService);
        intent.putExtras(bundle);
        setResult(RESULT_OK,intent);
        finish();
    }
}
